package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.IDistrictService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServImpl implements IAddressService {

    @Value("${user.address.maxCount}")
    private Integer maxCount;

    @Autowired
    private IDistrictService iDistrictService;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Void addNewAddress(Integer uid, String username, Address address) {
        //首先判断收货地址数量是否超出限制
        Integer count = addressMapper.countByUid(uid);
        if(count>=maxCount){
            throw new AddressCountLimitException("收货地址已达到最大限制！");
        }
        //判断是否是第一次新增地址，若是则将地址设置为默认收货地址
        address.setUid(uid);
        Integer isDefault = count== 0 ?1:0;//1表示默认地址，0表示不默认
        address.setIsDefault(isDefault);

        //通过地区code获取地区名称
        String provinceName = iDistrictService.getNameByCode(address.getProvinceCode());
        String cityName = iDistrictService.getNameByCode(address.getCityCode());
        String areaName = iDistrictService.getNameByCode(address.getAreaCode());

        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());

        Integer rows = addressMapper.insert(address);
        if(rows !=1){
            throw new InsertException("新增收货地址时发生数据库异常！");
        }

        return null;
    }

    @Override
    public List<Address> findInfoByUid(Integer uid) {
        List<Address> addressesInfo = addressMapper.findInfoByUid(uid);
        for (Address address: addressesInfo) {
            //address.setAid(null);
            //address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setIsDefault(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return addressesInfo;
    }

    @Override
    public void updateIsDefault(Integer uid,Integer aid, String username) {
        Address result = addressMapper.findByAid(aid);
        if(result==null){
            throw new AddressNotFoundException("收货地址不存在！");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("收货数据拒绝访问");
        }
        Integer rows = addressMapper.updateNoDefault(uid);
        if(rows<1){
            throw new UpdateException("更新数据过程中产生未知异常！");
        }

        rows = addressMapper.updateDefault(aid,username,new Date());
        if (rows!=1){
            throw new UpdateException("更新数据过程中产生未知异常！");
        }
    }

    @Override
    public void deleteAddressByAid(Integer aid, Integer uid,String username) {
        Address result = addressMapper.findByAid(aid);
        if(result==null){
            throw new AddressNotFoundException("收货地址不存在！");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("收货数据拒绝访问");
        }
        //当该用户的收货地址数量为0时，无法删除则直接终止该方法
        Integer rows = addressMapper.countByUid(uid);
        if(rows == 0){
            return;
        }

        rows = addressMapper.deleteByAid(aid);
        //判断删除的收货地址是否为默认地址，若是则将剩余地址中最近更改的一条地址设置为默认地址
        if(result.getIsDefault()==1){
            Address address = addressMapper.findLastModified(uid);
            addressMapper.updateDefault(address.getAid(),username,new Date());
        }
        if (rows!=1){
            throw new DeleteException("删除数据过程中产生未知异常！");
        }
    }

    @Override
    public Address findAddressByUid(Integer uid, Integer aid) {
        Address address = addressMapper.findByAid(aid);
        if (address==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if(!address.getUid().equals(uid)){
            throw new AccessDeniedException("访问权限受限");
        }
        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setModifiedTime(null);
        address.setModifiedUser(null);
        address.setCreatedTime(null);
        address.setCreatedUser(null);
        return address;
    }
}
